/*
 * AdminViewImpl.java
 */
package ATBMS.view.manager;

import ATBMS.modles.Flight;
import ATBMS.control.MainApp;
import ATBMS.control.ManagerControl;
import java.text.SimpleDateFormat;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 * The application's main frame.
 */
public class ManagerViewImpl extends FrameView
        implements ManagerViewInterface {

    public String getSelectedFlightCode() {
        int sel = flightsTable.getSelectedRow();
        if (sel == -1) {
            return "";
        }

        return (String) flightModle.getValueAt(sel, 1);
    }

    public Flight showEditFlightDialog(Flight flight) {
        FlightDialogInterface nfi = new EditFlightDialog(MainApp.getApplication().getMainFrame(), flight);
        return nfi.doModal();
    }

    public Flight showNewFlightDialog() {
        FlightDialogInterface nfi = new NewFlightDialog(MainApp.getApplication().getMainFrame());
        return nfi.doModal();
    }
    private DefaultTableModel flightModle;
    private ManagerControl mc;

    public void alert(String s) {
        JOptionPane.showMessageDialog(null, s);
    }

    public void hide() {
        MainApp.getApplication().hide(this);
    }

    public void show() {
        MainApp.getApplication().show(this);
    }

    public void listFlight(List<Flight> flights) {
        //Clear data
        flightModle.setRowCount(0);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //Add new data to usersModel
        for (Flight f : flights) {
            String gooffString = dateFormat.format(f.getFgooff());
            String arrivalString = dateFormat.format(f.getFarrival());
            flightModle.addRow(new Object[]{
                        f.getFdest(),
                        f.getFcode(),
                        f.getFairline(),
                        gooffString,
                        arrivalString,
                        f.getFbusload(),
                        f.getFequip(),
                        f.getFprice()
                    });
        }
        //update UI
        flightModle.fireTableDataChanged();
    }

    public ManagerViewImpl(SingleFrameApplication app, ManagerControl mc) {
        super(app);
        this.mc = mc;
        flightModle = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
// public void alert(String s);
        flightModle.addColumn("Destnation");
        flightModle.addColumn("Flight");
        flightModle.addColumn("Airline");
        flightModle.addColumn("Go-Off");
        flightModle.addColumn("Arrival");
        flightModle.addColumn("Busload");
        flightModle.addColumn("Equip.");
        flightModle.addColumn("Price");
        initComponents();
        getFrame().addWindowListener(new java.awt.event.WindowAdapter() {

            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                MainApp.getApplication().exit();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        flightsTable = new javax.swing.JTable();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        newUserMenuItem = new javax.swing.JMenuItem();
        editFlightMenuItem = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        flightsTable.setModel(flightModle);
        flightsTable.setName("flightsTable"); // NOI18N
        flightsTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        flightsTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(flightsTable);
        flightsTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ATBMS.control.MainApp.class).getContext().getResourceMap(ManagerViewImpl.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        newUserMenuItem.setAction(mc.getNewFlightAction());
        newUserMenuItem.setText(resourceMap.getString("newUserMenuItem.text")); // NOI18N
        newUserMenuItem.setName("newUserMenuItem"); // NOI18N
        fileMenu.add(newUserMenuItem);

        editFlightMenuItem.setAction(mc.getEditFlightAction());
        editFlightMenuItem.setText(resourceMap.getString("editFlightMenuItem.text")); // NOI18N
        editFlightMenuItem.setName("editFlightMenuItem"); // NOI18N
        fileMenu.add(editFlightMenuItem);

        jMenuItem1.setAction(mc.getDelFlightAction());
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        fileMenu.add(jMenuItem1);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(ATBMS.control.MainApp.class).getContext().getActionMap(ManagerViewImpl.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("about")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem editFlightMenuItem;
    private javax.swing.JTable flightsTable;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newUserMenuItem;
    // End of variables declaration//GEN-END:variables
}
